package myapp.baselibrary.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by yang2 on 2017/6/19.
 */

class AlertController {
    private AlertDialog mDialog;

    private Window mWindow;

    private DialogViewHelper mDialogViewHelper;

    public AlertController(AlertDialog alertDialog, Window window) {

        this.mDialog = alertDialog;

        this.mWindow = window;

    }

    public void setDialogViewHelper(DialogViewHelper viewHelper) {
        this.mDialogViewHelper = mDialogViewHelper;
    }

    public AlertDialog getDialog() {
        return mDialog;
    }

    public Window getWindow() {
        return mWindow;
    }

    public void setText(int i, CharSequence charSequence) {
        mDialogViewHelper.setText(i,charSequence);
    }

    public <T extends View>T getView(int i) {
       return mDialogViewHelper.getView(i);
    }

    public void setOnclickListener(int i, View.OnClickListener onClickListener) {
        mDialogViewHelper.setOnclickListener(i,onClickListener);
    }

    public static class AlertParams {
        public Context mContext;
        public int mThemeResId;
        public boolean mCancelable =true;
        public DialogInterface.OnCancelListener mOnCancelListener;
        public DialogInterface.OnDismissListener mOnDismissListener;
        public DialogInterface.OnKeyListener mOnKeyListener;
        public View mView;
        public int mViewLayoutResId;

        public SparseArray<CharSequence> mTextArray = new SparseArray<>();

        public SparseArray<View.OnClickListener> mClickArray = new SparseArray<>();
        public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
        public int  mAnimations = 0;
        public int mGravity = Gravity.CENTER;
        public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;

        public AlertParams(Context context, int themeResId) {
            this.mContext = context;
            this.mThemeResId = themeResId;
        }

        public void apply(AlertController mAlert) throws IllegalAccessException {

            DialogViewHelper viewHelper =null;
            if (mViewLayoutResId!=0){
                viewHelper = new DialogViewHelper(mContext,mViewLayoutResId);
            }
            if (mView !=null) {
                viewHelper = new DialogViewHelper();
                viewHelper.setContentView(mView);
            }

            if (viewHelper==null){
                throw new IllegalAccessException("please setContentView");
            }

            mAlert.getDialog().setContentView(viewHelper.getContentView());

            int textArraySize = mTextArray.size();
            for (int i= 0 ; i<textArraySize;i++ ){
                viewHelper.setText(mTextArray.keyAt(i),mTextArray.valueAt(i));
            }


            int  clickArraySize = mClickArray.size();

            for (int i= 0 ; i<clickArraySize;i++ ) {
                viewHelper.setOnclickListener(mClickArray.keyAt(i), mClickArray.valueAt(i));
            }
            mAlert.setDialogViewHelper(viewHelper);
            Window window = mAlert.getWindow();
            window.setGravity(mGravity);
            if (mAnimations!=0){
                window.setWindowAnimations(mAnimations);
            }
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = mWidth;
            params.height = mHeight;
            window.setAttributes(params);



        }
    }
}
